<?php
namespace app\api\controller\foodcash;

use app\api\controller\foodcash\Controller;
use app\api\model\foodcash\User as UserModel;
use app\api\model\foodcash\Record as RecordModel;
use app\api\model\foodcash\ShopClerk as ShopClerkModel;
use app\api\model\Setting;
use hema\wechat\Pay as WxPay;

/**
 * 用户管理
 */
class User extends Controller
{
    /**
     * 检测微信充值状态
     */
    public function checkRechargeStatus($order_no)
    {
		if($record = (new RecordModel)->where(['order_no' => $order_no, 'status' => 10])->find()){
            //获取小程序配置的支付参数
            $payment = Setting::getItem('payment');
            $wxpay = new WxPay($payment['wx']);
            $res = $wxpay->orderquery($record['order_no']);
            if($res == 'SUCCESS'){
               //支付成功
               $user = UserModel::get($record['user_id']);
               $user->money = ['inc',$record['money']];
               $user->save();
               $record->status = 20;
               $record->save();
            }
            return $this->renderSuccess([
                'pay_status' => $res,
                'order_no' => $order_no
            ]);
		}
		return $this->renderSuccess([
            'pay_status' => 'ERROR',
            'order_no' => $order_no
        ]);
    }
    
    /**
     * 会员充值
     */
    public function recharge()
    {
        $data = $this->request->post();
        $model = UserModel::get($data['user_id']);
		if($res = $model->recharge($data)){
			return $this->renderSuccess($res);
		}
		$error = $model->getError() ?: '充值失败';
        return $this->renderError($error);
    }
    
    /**
     * 订单列表
     */
    public function lists($search='')
    {
        $model = new UserModel;
		if($list = $model->getList(10,'all',$search)){
        	return $this->renderSuccess(compact('list'));
        }
        return $this->renderError('会员列表获取失败');
    }
    
    /**
     * 登录
     */
    public function login()
    {
		$model = new ShopClerkModel;
		if($user = $model->login($this->request->post())){
			return $this->renderSuccess(compact('user'));
		}
		$error = $model->getError() ?: '登录失败';
        return $this->renderError($error);
    }
    
    /**
     * 根据手机号获取会员详情
     */
    public function phone($phone)
    {
        if($user = UserModel::with(['addressDefault'])->where(['phone' => $phone])->find()){
            return $this->renderSuccess(compact('user'));
        }
        return $this->renderError('会员不存在');
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    



	/**
     * 修改密码
	*/
	public function renew()
	{
		$user = $this->request->post();
		if($user['admin'] > 10){
			$model = new ShopClerkModel;
			if($model->renew($user)){
				return $this->renderMsg('修改成功');
			}
			$error = $model->getError() ?: '修改失败';
			return $this->renderError($error);
		}
		$model = new UserModel;
		if($model->renew($user)){
			return $this->renderMsg('修改成功');
		}
		$error = $model->getError() ?: '修改失败';
        return $this->renderError($error);
	}

    /**
     *  获取用户详情 
    */
    public function getUserId($user_id)
    {
        $detail = UserModel::get($user_id);   // 用户信息
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 获取用户手机号
     */
    public function getPhoneNumber()
    {
        $model = new UserModel;
        if ($model->getPhoneNumber($this->request->post(),$this->user)) {
            return $this->renderMsg('手机号绑定成功');
        }
        return $this->renderError('手机号绑定失败');
    }

	/**
     * 统计待处理订单数量
     */
	public function orderCount()
    {
        // 订单总数
        $model = new OrderModel;
        $orderCount = [
            'shop' => $model->getOrderClassCount('shop',$this->shop_id,'','help'),
            'delivery' => $model->getOrderClassCount('delivery',$this->shop_id,'','help'),
			'collection' => $model->getOrderClassCount('collection',$this->shop_id,'','help'),
			'refund10' => $model->getOrderClassCount('refund10',$this->shop_id,'','help')
        ];
        return $this->renderSuccess(compact('orderCount'));
    }

}
